import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.*;

public class SingleFieldRowUtil {
    public static class Item {
        private int id;
        private String text;

        public Item(int id, String text) {
            this.id = id;
            this.text = text;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

    }

    private SingleFieldRowUtil() {
    }

    public static List<Item> getItems(Document document) {
        List<Item> items = new ArrayList<Item>();
        Element rootEl = document.getDocumentElement();
        NodeList rootChildNodes = rootEl.getChildNodes();
        int itemIndex = 0;
        for (int i = 0; i < rootChildNodes.getLength(); i++){
            Node rootChildNode = rootChildNodes.item(i);
            if (rootChildNode instanceof Text) continue;
            itemIndex++;
            Element documentEl = (Element) rootChildNode;
            Node fieldNode = getFieldNode(documentEl);
            String fieldContent = fieldNode.getTextContent();
            Item item = new Item(itemIndex, fieldContent);
            items.add(item);
        }
        return items;
    }

    private static Node getFieldNode(Node node) {
        NodeList docChildNodes = node.getChildNodes();
        Node fieldNode = null;
        for (int j = 0; j < docChildNodes.getLength(); j++){
            Node docChildNode = docChildNodes.item(j);
            if (docChildNode instanceof Text) continue;
            fieldNode = docChildNode;
            break;
        }
        return fieldNode;
    }
}
